var $ = require('jquery');
var url = require('url');
var http = require('http');
var iconv = require('iconv-lite')
var webInterface = require('./webInterface');
var log4js = require('log4js');
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('logs/sellerInfoTest.log'), 'sellerInfo');
var logger = log4js.getLogger('sellerInfo');


var mdskipApi =  'http://mdskip.taobao.com/extension/seller_info.htm?';
var tbskipurl = 'http://tbskip.daily.taobao.net/json/seller_info.htm?callback=jsonpSellerInfo&user_num_id=2033188194&user_tag=642879520&shop_start=1307431182000';

exports.mdskipApi = mdskipApi;
exports.tbskipApi = 'http://tbskip.taobao.com/json/seller_info.htm';

var jsonpSellerInfo = function(jsonObj){
	return jsonObj;
}

exports.verify = function(tbskipurl, callback){
	logger.error('readUrl:	' + tbskipurl);
	var mdskipurl = convertToMdskipUrl(tbskipurl);
		webInterface.sendRequest(mdskipurl, tbskipurl, function(mdskipres, tbskipres){
			try{
//				tbskipres = tbskipres.replace(/\\"/g, "\"");
				var tbskipResult = eval(tbskipres);
				var mdskipResult = eval(mdskipres)
				tbskipResult = (tbskipResult.replace(/<!--[/!]*?[^<>]*?>/g, ''))
				mdskipResult = mdskipResult.replace(/<!--[/!]*?[^<>]*?>/g, '')
				
				
				if(tbskipResult != mdskipResult){
					logger.error("NotEqual	" + mdskipurl + "		" + tbskipurl);
				}else{
					logger.error('equals');
				}
			}catch(e){
				logger.error("Exception:" + mdskipurl + "	" + tbskipurl, e);
			}
			
			if((typeof callback != 'undefined') && callback != null){
				callback();
			}
		});
}


function convertToMdskipUrl(tbskipurl){
	var tbskipRequest = url.parse(tbskipurl);
	var mdskipurl = mdskipApi + tbskipRequest.query + '&debug=true';
	return mdskipurl;
}
